package com.netbao.sys.controller;

import com.netbao.model.PageBean;
import com.netbao.model.PageResult;
import com.netbao.model.Result;
import com.netbao.sys.model.Role;
import com.netbao.sys.service.ResourceService;
import com.netbao.sys.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sys/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @GetMapping("/page")
    public String toPage(){
        return "/sys/role";
    }
    @GetMapping("/list")
    @ResponseBody
    private PageResult list(PageBean pageBean, String roleName){
        return roleService.list(pageBean,roleName);
    }

    @PostMapping("/save")
    @ResponseBody
    private Result save(Role role){
        return roleService.save(role);
    }

    @PostMapping("/delete")
    @ResponseBody
    private Result delete(Long id){
        return roleService.delete(id);
    }
    @Autowired
    private ResourceService resourceService;
    @GetMapping("/getres")
    @ResponseBody
    public Object getRes(Long id){
//        System.out.println("controller-------------"+id);
        return resourceService.getRolesidByRes(id);
    }

    @PostMapping("/saveres")
    @ResponseBody
    public Result getRes(String menuIds, Long roleId){
        return roleService.saveRes(roleId,menuIds);
    }

    @PostMapping("/saveres1")
    @ResponseBody
    public Result getRes1(@RequestParam(value="menuArrIds[]") String[] menuArrIds, Long roleId){
        List<String> strings = Arrays.asList(menuArrIds);
        Map<String, Object> map = new HashMap<>();
        map.put("roleId",roleId);
        map.put("menuArrIds",strings);
        return roleService.saveRes1(map);
    }


}
